package mywebapp.dao.visiteur;

import mywebapp.WebApp;
import mywebapp.dao.Champ;
import mywebapp.dao.ETypeSimple;
import mywebapp.dao.Entity;
import mywebapp.dao.TypeEntity;
import mywebapp.dao.TypeSimple;

public class VisiteurModelBDD implements VisiteurEntity{

	private String requeteF = "";
	
	public String visiter(WebApp webApp) {
		String requete = "";
		requete += "SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\";\n";
		requete += "SET time_zone = \"+00:00\";\n\n";
		requete += "CREATE DATABASE IF NOT EXISTS `"+ webApp.getName().toLowerCase() + "` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;\n";
		requete += "USE `"+ webApp.getName().toLowerCase() +"`;\n\n";
		for(Object o : webApp.getEntities()){
			Entity e = (Entity)o;
			requete += visiter(e);
		}
		return requete + requeteF;
	}

	@Override
	public String visiter(Entity e) {
		String requete = "";
		String primary = "\t PRIMARY KEY (`ID`)";
		String foreign = "";
		String LinkTable = "";
		requete += "CREATE TABLE IF NOT EXISTS `" + e.getName().toLowerCase() + "` ( \n";
		requete += "\t ID BIGINT NOT NULL AUTO_INCREMENT, \n";
		for(Object o1 : e.getChamps())
		{
			String nameType = "";
			Champ c = (Champ)o1;
			if(c.getType() instanceof TypeSimple){
				TypeSimple t = (TypeSimple)c.getType();
				switch(t.getType().getValue()){
				case ETypeSimple.BIGINT :
					nameType = "BIGINT NOT NULL";
					break;
				case ETypeSimple.STRING :
					nameType = "VARCHAR(255) NOT NULL ";
					break;
				case ETypeSimple.DATE:
					nameType ="DATE NOT NULL ";
					break;
				case ETypeSimple.FLOAT:
					nameType =" FLOAT NOT NULL";
					break;
				case ETypeSimple.INT:
					nameType =" INT NOT NULL";
					break;
				}
			}else {
				nameType = "BIGINT NOT NULL";
			}
			
			if(c.isIsList()){
				LinkTable += "CREATE TABLE IF NOT EXISTS `" + e.getName().toLowerCase()+"_"+ c.getName().toLowerCase()+"` ( \n";
				LinkTable += "\t `ID_"+ e.getName().toLowerCase() + "` BIGINT NOT NULL, \n";
				if(c.getType() instanceof TypeEntity){
					TypeEntity type = (TypeEntity)c.getType();
					LinkTable += "\t `"+ c.getName().toLowerCase() + "` "+ nameType +" \n";
					foreign += "ALTER TABLE `" + e.getName().toLowerCase()+"_"+ c.getName().toLowerCase()+"` \n"+
							   "ADD CONSTRAINT `FK01_" + e.getName().toLowerCase()+"_"+ c.getName().toLowerCase()+"` " +
							   		"FOREIGN KEY (`"+c.getName().toLowerCase()+"`) REFERENCES `"+ type.getType().getName().toLowerCase()+"` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION;\n";

				}else {
					LinkTable += "\t `" + c.getName().toLowerCase() + "` " + nameType + " \n";
				}
				foreign += "ALTER TABLE `" + e.getName().toLowerCase()+"_"+ c.getName().toLowerCase()+"` \n"+
						   "ADD CONSTRAINT `FK02_" + e.getName().toLowerCase()+"_"+ c.getName().toLowerCase()+"` " +
						   		"FOREIGN KEY (`ID_"+ e.getName().toLowerCase() + "`) REFERENCES `"+  e.getName().toLowerCase()+"` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION;\n";

			
				LinkTable += "); \n";
			}else {
				if(c.getType() instanceof TypeEntity){
					requete += "\t `" + c.getName().toLowerCase() + "ID` " +  nameType +",\n";
					TypeEntity type = (TypeEntity)c.getType();
					foreign += "ALTER TABLE `" + e.getName().toLowerCase()+"` \n"+
							   "ADD CONSTRAINT `FK_" + e.getName().toLowerCase()+"_"+ c.getName().toLowerCase()+"` " +
							   		"FOREIGN KEY (`"+c.getName().toLowerCase()+"ID`) REFERENCES `"+  type.getType().getName().toLowerCase()+"` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION;\n";

				
				}
				else 
					requete += "\t `" + c.getName().toLowerCase() + "` " +  nameType +",\n";
			}
			
		}
		requete += primary;
		requeteF += foreign + "\n";
		requete += "\n)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;\n\n";
		requete += LinkTable;
		requete += "\n";
		return requete;
	}

}
